﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data;
using Model;

namespace Controller
{
   public class cntrForo
    {

       Foro objForo;

       public cntrForo()
       {
       }

       #region Metodos

       #region Salvar

       public Boolean Save(DS_Lawyer.ForoRow pForo)
       {
           Boolean flagReturn = false;
           if (pForo.idForo == string.Empty)
           {
               this.objForo = new Foro();
               this.objForo.IdForo = System.Guid.NewGuid().ToString();
               this.objForo.Descricao = pForo.descricao;
               this.objForo.Telefone = pForo.telefone;
               this.objForo.Endereco = Endereco.RetriveObject(pForo.idEndereco);

               try
               {
                   flagReturn = this.objForo.Persist();
               }
               catch (Exception ex)
               {
                   throw ex;
               }
           }
           else
           {
               this.objForo = Foro.RetriveObject(pForo.idForo);
               this.objForo.Descricao = pForo.descricao;
               this.objForo.Telefone = pForo.telefone;
               this.objForo.Endereco = Endereco.RetriveObject(pForo.idEndereco);
               try
               {
                   flagReturn = this.objForo.Update();
               }
               catch (Exception ex)
               {

                   throw ex;
               }
           }
           return flagReturn;
       }
       #endregion

       #region Delete
       public static Boolean Delete(String pID)
       {
           Boolean flagReturn = false;
           try
           {
               flagReturn = Foro.Delete(pID);
           }
           catch (Exception e)
           {
               throw e;
           }
           return flagReturn;
       }


       #endregion

       #region Select's

       #region SelecByID
       public DS_Lawyer.ForoRow SelectByID(string pID)
       {
           DS_Lawyer.ForoDataTable dtForo = new DS_Lawyer.ForoDataTable();
           DS_Lawyer.ForoRow rowForo = dtForo.NewForoRow();

           try
           {
               this.objForo = Foro.RetriveObject(pID);
               rowForo.idForo = pID;
               rowForo.idEndereco = this.objForo.Endereco.IdEndereco;
               rowForo.descricao = this.objForo.Descricao;
               rowForo.telefone = this.objForo.Telefone;

           }
           catch (Exception ex)
           {

               throw ex;
           }

           return rowForo;
       }
       #endregion

       #region Select
       public static DS_Lawyer.ForoDataTable Select()
       {
           DS_Lawyer.ForoDataTable dtForo = new DS_Lawyer.ForoDataTable();
           try
           {
               IList listForo = Foro.RetriveObjects();
               for (int i = 0; i < listForo.Count; i++)
               {
                   Foro objForo = (Foro)listForo[i];
                   dtForo.AddForoRow(objForo.IdForo, objForo.Endereco.IdEndereco, 
                       objForo.Telefone, objForo.Descricao);
               }
           }
           catch (Exception ex)
           {
               //Tratar mensagem de erro aqui!
               throw ex;

           }
           return dtForo;
       }
       #endregion

       #region SelectByNome
       public static DS_Lawyer.ForoDataTable SelectByNome(string pNome)
       {
           DS_Lawyer.ForoDataTable dtForo = new DS_Lawyer.ForoDataTable();
           try
           {
               IList listForos = Foro.RetriveObjects(pNome);
               for (int i = 0; i < listForos.Count; i++)
               {
                   Foro objForo = (Foro)listForos[i];
                   dtForo.AddForoRow(objForo.IdForo, objForo.Endereco.IdEndereco, objForo.Telefone,
                       objForo.Descricao);
               }
           }
           catch (Exception ex)
           {
               //Tratar mensagem de erro aqui!
               throw ex;

           }
           return dtForo;
       }
       #endregion


       #endregion

       #endregion

    }
}
